/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.newclasses;

import java.sql.Timestamp;
import java.util.Vector;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

	/*
	 * Classe para processar o arquivo XML.
	 * Referência: http://www.ibm.com/developerworks/opensource/library/x-android/
	 * */
public class Chat {

	public static Vector<ChatMessage> parse(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			ChatXMLHandler handler = new ChatXMLHandler();
			xr.setContentHandler(handler);
			xr.parse(xml);
			return handler.getChatMessages();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static class ChatXMLHandler extends DefaultHandler {
		private StringBuilder builder;
		private Timestamp dateSent;
		private String sender;
		private String msg;
		private Vector<ChatMessage> vetMsg;
		
		public Vector<ChatMessage> getChatMessages() {
			return vetMsg;
		}
		
		@Override
		public void startDocument() throws SAXException {
			super.startDocument();
			builder = new StringBuilder();
			vetMsg = new Vector<ChatMessage>();
		}
		
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			super.startElement(uri, localName, qName, attributes);
			if (localName.equalsIgnoreCase("message")) {
				dateSent = null;
				sender = null;
				msg = null;
			}
			builder.setLength(0);
		}
		
		@Override
		public void characters(char[] ch, int start, int length) throws SAXException {
			super.characters(ch, start, length);
			builder.append(ch, start, length);
		}
		
		@Override
		public void endElement(String uri, String localName, String qName) throws SAXException {
			super.endElement(uri, localName, qName);
			
			String value = builder.toString();
			
			if (localName.equals("date")){
				dateSent = Timestamp.valueOf(value);
			}else if(localName.equals("sender")){
				sender = value;
			}else if(localName.equals("body")){
				msg = value;
			}else if(localName.equals("message")){
				if(dateSent != null && sender != null && msg != null){
					ChatMessage cm = new ChatMessage(sender, msg, dateSent);
					vetMsg.add(cm);
				}
				dateSent = null;
				sender = null;
				msg = null;
			}
		}
		
	}
	
}
